package com.yuntsg.ruiijn.service;

import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.springframework.stereotype.Service;
import org.springframework.util.ResourceUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.util.Base64;

@Service
public class QrcodeService {
    public String getWxPayQrcodeBase64(String url) {
        try {
            QrConfig config = new QrConfig();
            config.setHeight(300);
            config.setWidth(300);
            config.setImg(ResourceUtils.getFile("classpath:logo.jpg"));
            config.setErrorCorrection(ErrorCorrectionLevel.H);
            BufferedImage bufferedImage = QrCodeUtil.generate(url, config);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(bufferedImage, "png", baos);
            return "data:image/png;base64," + Base64.getEncoder().encodeToString(baos.toByteArray());
        } catch (Exception ex) {
            ex.printStackTrace();
            return "";
        }
    }
}
